package com.ruoyi.web.controller.teaching;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.controller.HandleVideoSrc;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.teaching.domain.PlatformTeaching;
import com.ruoyi.teaching.service.IPlatformTeachingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 名师视频Controller
 *
 * @author ruoyi
 * @date 2021-12-07
 */
@RestController
@RequestMapping("/teaching/teaching")
public class PlatformTeachingController extends BaseController
{
    @Autowired
    private IPlatformTeachingService platformTeachingService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询名师视频列表
     */
    @PreAuthorize("@ss.hasPermi('teaching:teaching:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlatformTeaching platformTeaching)
    {
        startPage();
        List<PlatformTeaching> list = platformTeachingService.selectPlatformTeachingList(platformTeaching);
        return getDataTable(list);
    }

    /**
     * 导出名师视频列表
     */
    @PreAuthorize("@ss.hasPermi('teaching:teaching:export')")
    @Log(title = "名师视频", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PlatformTeaching platformTeaching)
    {
        List<PlatformTeaching> list = platformTeachingService.selectPlatformTeachingList(platformTeaching);
        ExcelUtil<PlatformTeaching> util = new ExcelUtil<PlatformTeaching>(PlatformTeaching.class);
        return util.exportExcel(list, "名师视频数据");
    }

    /**
     * 获取名师视频详细信息
     */
    @PreAuthorize("@ss.hasPermi('teaching:teaching:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(platformTeachingService.selectPlatformTeachingById(id));
    }



    /**
     * 新增名师视频
     */
    @PreAuthorize("@ss.hasPermi('teaching:teaching:add')")
    @Log(title = "名师视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlatformTeaching platformTeaching)
    {
        Map<String, String> val = configService.getVal();
        platformTeaching.setVideoCover(HandleVideoSrc.handle(val.get("key"),val.get("id"),val.get("region"),val.get("bn"),platformTeaching.getTeachUrl()));
        return toAjax(platformTeachingService.insertPlatformTeaching(platformTeaching));
    }


    /**
     * 修改名师视频
     */
    @PreAuthorize("@ss.hasPermi('teaching:teaching:edit')")
    @Log(title = "名师视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformTeaching platformTeaching)
    {
        Map<String, String> val = configService.getVal();
        platformTeaching.setVideoCover(HandleVideoSrc.handle(val.get("key"),val.get("id"),val.get("region"),val.get("bn"),platformTeaching.getTeachUrl()));
        platformTeaching.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(platformTeachingService.updatePlatformTeaching(platformTeaching));
    }

    /**
     * 删除名师视频
     */
    @PreAuthorize("@ss.hasPermi('teaching:teaching:remove')")
    @Log(title = "名师视频", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(platformTeachingService.logicDeletePlatformTeaching(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
